<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Service;
use App\Models\ServiceType;
use App\Imports\ServiceImport;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\ServiceExport;

class ServiceController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $query = Service::query();
        if($request->keyword) {
            $query->where('name','like','%'.$request->keyword.'%');
        }
        $list  = $query->paginate(20);
        $types = ServiceType::get();
        return view('admin.service.index',compact('list','types'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $types = ServiceType::get();
        return view('admin.service.add',compact('types'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->except('_token');
        $data['code'] = 'S'.date('YmdHis');
        if($data['price_type']==2) {
            $data['price'] = $data['gongshi']*$data['gongshi_fee'];
        }
        $rs   = Service::create($data);
        if($rs) {
            return redirect('zadmin/services');
        }
        return back();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = Service::find($id);
        $types = ServiceType::get();
        return view('admin.service.edit',compact('data','types'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->except('_token','_method');
        
        $rs   = Service::where('id',$id)->update($data);
        if($rs) {
            return redirect('zadmin/services');
        }
        return back()->withInput();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $rs = Service::destroy($id);
        if ($rs) {
            return redirect('zadmin/services');
        }
        return back();
    }

    public function import(Request $request)
    {
        Excel::import(new ServiceImport, request()->file('excel'));
        return back();
    }

    public function bathDel(Request $request)
    {
        $rs['status'] = false;
        $ids = $request->ids;
        $flag = Service::where('id',$ids)->delete();
        if($flag) {
            $rs['status'] = true;
            return response()->json($rs);
        }
         return response()->json($rs);
    }

    //导出工单。
    public function serviceExport(Request $request)
    {
        $param = [];
        if($request->client_id) {
            $param['keyword'] = (int)$request->keyword;
        }
        return Excel::download(new ServiceExport($param), '维修项目.xlsx');   
    }
}
